/* vim:set noet ts=4: */
/** 
 * scim-python
 * 
 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id: $
 */
#include "../config.h"
#include "scim-python.h"

using namespace scim;
using namespace std;

static vector <IMEngineFactoryPointer> _factorys;
static int _use_count = 0;

PyMODINIT_FUNC
init_scim(void) 
{
	PyObject* m;
	
	m = Py_InitModule3("_scim", NULL,
						"SCIM.");
	
	if (m == NULL) {
		PyErr_Print ();
		return;
	}
	
	init_event (m);
	init_property (m);
	init_config (m);
	init_engine (m);
	init_factory (m);
	init_attribute (m);
	init_lookup_table (m);
}

static PyObject *
Py_CallFunction (char *module, char *function, PyObject *args)
{
	PyObject *pName = NULL;
	PyObject *pModule = NULL;
	PyObject *pFunc = NULL;
	PyObject *pValue = NULL;

	pName = PyString_FromString(module);
	if (pName == NULL)
		goto _failed_out;

	pModule = PyImport_Import(pName);
	Py_DECREF(pName);

	if (pModule == NULL)
		goto _failed_out;
	
	pFunc = PyObject_GetAttrString (pModule, function);
	
	if (pFunc == NULL)
		goto _failed_out;
	
	pValue = PyObject_CallObject(pFunc, args);	

	if (pValue == NULL)
		goto _failed_out;

	Py_DECREF(pFunc);
	Py_DECREF(pModule);
	
	return pValue;
	
_failed_out:
	PyErr_Print ();
	Py_XDECREF (pFunc);
	Py_XDECREF (pModule);
	Py_INCREF (Py_None);
	return Py_None;

}

extern "C" void
scim_module_init (void)
{
	bindtextdomain (GETTEXT_PACKAGE, SCIM_PYTHON_LOCALEDIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");

	if (!Py_IsInitialized ()) {
		char pythonpath[256] = "PYTHONPATH="SCIM_PYTHON_DATADIR;
		char *old_pythonpath = getenv ("PYTHONPATH");
		if (old_pythonpath != NULL) {
			strncat (pythonpath, ":", sizeof (pythonpath));
			strncat (pythonpath, old_pythonpath, sizeof (pythonpath));
		}
		putenv (pythonpath);
		Py_Initialize ();
	}
	_use_count ++;
}

extern "C" void
scim_module_exit (void)
{
	_use_count --;
	if (_use_count == 0) {
		_factorys.clear ();
		Py_Finalize ();
	}
}


#ifndef DISABLE_IMENGINE
extern "C" unsigned int 
scim_imengine_module_init (const ConfigPointer &config)
{
	PyObject *pValue = NULL;
	PyObject *pArgs = NULL;
	PyObject *pyconfig = NULL;
	int size;

	pyconfig = PyConfig_New (config);
	Py_DECREF (pyconfig);
	pyconfig = PyConfig_New (config);

	pArgs = Py_BuildValue("(O)", pyconfig);
	pValue = Py_CallFunction ("engine", "query_engines", pArgs);	
	
	Py_DECREF (pArgs);
	Py_DECREF (pyconfig);
	if (PyList_Check (pValue)) {
		PyObject * pTuple = PyList_AsTuple (pValue);
		Py_DECREF (pValue);
		pValue = pTuple;
	}
	if (!PyTuple_Check (pValue))
		return 0;

	size = PyTuple_Size (pValue);

	for (int i = 0; i < size; i++) {
		IMEngineFactoryPointer p = PyIMEngineFactory::from_pyobject(PyTuple_GetItem(pValue, i));
		_factorys.push_back (p);
	}

	Py_DECREF (pValue);
	return size;
}

extern "C" IMEngineFactoryPointer 
scim_imengine_module_create_factory (uint32 engine)
{
	PyIMEngineFactory *factory = NULL;

	if ( engine < 0 || engine >= _factorys.size ())
		return IMEngineFactoryPointer (0);

	return _factorys[engine];
}
#endif //DISABLE_IMENGINE

#ifndef DISABLE_SETUPUI
#include <gtk/gtk.h>
#include <pygobject.h>



extern "C" GtkWidget * 
scim_setup_module_create_ui (void)
{
	static GtkWidget *window = NULL;
	PyObject *pValue = NULL;

	if (window != NULL)
		return window;

	
	pValue = Py_CallFunction ("setupui", "create_ui", NULL);	

	if (pValue != Py_None) {
		window = GTK_WIDGET (pygobject_get (pValue));
		gtk_object_ref (GTK_OBJECT (window));
	}

	Py_DECREF(pValue);
	return window;
}

extern "C" String
scim_setup_module_get_category (void)
{
	return String ("IMEngine");
}

extern "C" String
scim_setup_module_get_name (void)
{
	return String (_("Python"));
}

extern "C" String
scim_setup_module_get_description (void)
{
	return String (_("Setup UI for Python IM engines."));
}

extern "C" void
scim_setup_module_load_config (const ConfigPointer &config)
{
	PyObject *pValue = NULL;
	PyObject *pArgs = NULL;

	pArgs = Py_BuildValue("(O)", PyConfig_New (config));
	pValue = Py_CallFunction ("setupui", "load_config", pArgs);	

	Py_DECREF (pArgs);
	Py_DECREF(pValue);
}

extern "C" void
scim_setup_module_save_config (const ConfigPointer &config)
{
	PyObject *pValue = NULL;
	PyObject *pArgs = NULL;

	pArgs = Py_BuildValue("(O)", PyConfig_New (config));
	pValue = Py_CallFunction ("setupui", "save_config", pArgs);	

	Py_DECREF (pArgs);
	Py_DECREF(pValue);
}

extern "C" bool
scim_setup_module_query_changed ()
{
	PyObject *pValue = NULL;
	bool retval = false;

	pValue = Py_CallFunction ("setupui", "query_changed", NULL);	
	if (pValue == Py_True)
		retval = true;

	Py_DECREF (pValue);

	return retval;
}

#endif //DISABLE_SETUPUI
